package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.adpter.SpeacialRecyerAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.SpResult;
import com.lanou.dllo.findfood.entity.Speacil_list;
import com.lanou.dllo.findfood.volley.VolleySington;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class SpecialFragment  extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recyclerView;
    private SpeacialRecyerAdpter adpter;
    private PullRefreshLayout swipeRefreshLayout;
    private int lastVisibleItem;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_special,null);

        return view;
    }

    //逻辑的方法在这里写
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
         init();
        requestData();

    }

   private void init(){

       recyclerView = (RecyclerView) getActivity().findViewById(R.id.fragmentSpView);
       GridLayoutManager m  = new GridLayoutManager(getContext(),1);
       recyclerView.setLayoutManager(m);
       adpter = new SpeacialRecyerAdpter();
       recyclerView.setAdapter(adpter);

       swipeRefreshLayout = (PullRefreshLayout) getActivity().findViewById(R.id.swipeRefreshLayout);
       swipeRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
           @Override
           public void onRefresh() {
               swipeRefreshLayout.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       swipeRefreshLayout.setRefreshing(false);
                   }
               }, 4000);
           }
       });





    }

   private void requestData(){
       String path = "http://api.wecook.cn/v3/cooking/recommend?timestamp=1445691784843&sign=75be8222607f22119fcf39e0196c6ebe&uid=502685&sort=1&wid=f06f82c3d0f511b0d7c1bd250e194106&page=1&batch=20&net=wifi&access=259eea423dee18c7b865b0777cd657cc";
       JsonObjectRequest request = new JsonObjectRequest(path, null, new Response.Listener<JSONObject>() {
           @Override
           public void onResponse(JSONObject response) {
               Log.i("lanou",response+ " fjklasjdfkalf");
               if (response!= null && response.has("result"));

               try {
                   JSONObject result = response.getJSONObject("result");

                   Gson gson = new Gson();
                      SpResult results = gson.fromJson(result.toString(),SpResult.class);
                      if (results != null ){
                          List<Speacil_list> lists = results.getList();
                          adpter.addData(lists);
                      }


               } catch (JSONException e) {
                   e.printStackTrace();
               }

           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               error.printStackTrace();
           }
       }){
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String, String> map = new HashMap<String, String>();
               map.put("Accept-Encoding", "");
               return map;
           }
       };

       VolleySington.getInstance().addRequest(request);
    }

    @Override
    public void onRefresh() {

    }
}
